package com.xuanzheng.company.servlet;

import com.google.gson.Gson;
import com.xuanzheng.company.entity.User;
import com.xuanzheng.company.service.SystemApplicationService;
import com.xuanzheng.company.service.impl.SystemApplicationServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/reviewApplication")
public class ReviewApplicationServlet extends HttpServlet {
    private final SystemApplicationService applicationService = new SystemApplicationServiceImpl();
    private final Gson gson = new Gson();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        Map<String, Object> result = new HashMap<>();
        
        // 检查用户会话
        HttpSession session = request.getSession(false);
        if (session == null || session.getAttribute("user") == null) {
            result.put("success", false);
            result.put("message", "请先登录");
            out.write(gson.toJson(result));
            return;
        }
        
        // 获取当前用户
        User user = (User) session.getAttribute("user");
        
        // 检查用户角色，必须是系统管理员
        if (user.getRole() != 2) {
            result.put("success", false);
            result.put("message", "只有系统管理员可以审核系统使用申请");
            out.write(gson.toJson(result));
            return;
        }
        
        // 获取审核参数
        String applicationId = request.getParameter("applicationId");
        String statusStr = request.getParameter("status");
        String rejectReason = request.getParameter("rejectReason");
        
        // 验证参数
        if (applicationId == null || applicationId.trim().isEmpty() || statusStr == null || statusStr.trim().isEmpty()) {
            result.put("success", false);
            result.put("message", "参数不完整");
            out.write(gson.toJson(result));
            return;
        }
        
        // 解析状态值
        int status;
        try {
            status = Integer.parseInt(statusStr);
            if (status != 1 && status != 2) {
                throw new NumberFormatException("状态值必须是1(通过)或2(拒绝)");
            }
        } catch (NumberFormatException e) {
            result.put("success", false);
            result.put("message", "状态值无效");
            out.write(gson.toJson(result));
            return;
        }
        
        // 如果拒绝原因为空，根据状态设置默认值
        if (rejectReason == null || rejectReason.trim().isEmpty()) {
            rejectReason = status == 1 ? "通过" : "拒绝";
        }
        
        // 执行审核
        boolean success = applicationService.reviewApplication(applicationId, status, rejectReason);
        
        if (success) {
            result.put("success", true);
            result.put("message", status == 1 ? "已通过申请" : "已拒绝申请");
        } else {
            result.put("success", false);
            result.put("message", "审核操作失败，请稍后重试");
        }
        
        out.write(gson.toJson(result));
    }
} 